package com.yanqu.road.server.cmd.wingroom.chakra.cross;

import com.yanqu.road.entity.wingroom.chakra.data.UserWingRoomChakra;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.WingRoomChakraProto.WingRoomChakraProto;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.config.grandchild.talent.utils.TalentResultUtils;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.wingroom.WingRoomChakraMgr;
import com.yanqu.road.server.manger.wingroom.WingRoomMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author ：zfr
 * @date ：2022/2/23 14:11
 */
@Cmd(code = GameProtocol.S_CHAKRA_VISIT_FROM_CROSS, desc = "厢房命盘-跨服拜访-拜访处理")
public class CrossChakraVisitCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        WingRoomChakraProto.WingRoomChakraVisitFromCrossReqMsg reqMsg = WingRoomChakraProto.WingRoomChakraVisitFromCrossReqMsg.parseFrom(packet.getBytes());
        WingRoomChakraProto.WingRoomChakraCrossBeVisitReqMsg.Builder crossReqMsg = WingRoomChakraProto.WingRoomChakraCrossBeVisitReqMsg.newBuilder();
        crossReqMsg.setVisitServerId(reqMsg.getVisitServerId());
        crossReqMsg.setVisitUserId(reqMsg.getVisitUserId());
        crossReqMsg.setBeVisitServerId(GameServer.getInstance().getServerId());
        crossReqMsg.setBeVisitUserId(reqMsg.getBeVisitUserId());
        try {
            //拜访处理
            UserWingRoomChakra userWingRoomChakra = WingRoomChakraMgr.crossVisitHandle(reqMsg);
            crossReqMsg.setRet(0);
            crossReqMsg.setBeVisitWindWaterLevel(userWingRoomChakra.getWindWaterLevel());
            crossReqMsg.setBeVisitAddRate(WingRoomChakraMgr.getVisitWindWaterAddRate(reqMsg.getBeVisitUserId()));
        } catch (BusinessException e) {
            crossReqMsg.setRet(e.getCode());
        }
        //通知跨服
        YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_CROSS_WING_ROOM_CHAKRA_BE_VISIT, crossReqMsg);
        GamePlayerMgr.sendPacket(0, message);
    }
}
